﻿using System;
using WiMo.Games;
using WiMo.Games.Inputs;
using WiMo.Games.Menus;

namespace Menus
{
    public class Game : IGame
    {
        private const int MNU_CLOSE = 100;
        private const int MNU_EXIT = 999;
        private IMenuComponent _mainMenu;

        public Game(IGameEngine engine)
        {
            Engine = engine;
        }

        public void Initialize() 
        {
            Engine.InputManager.MenuBarBackgroundColor = Color.Black;
            Engine.InputManager.SoftMenuFontColor = Color.White;
            Engine.Display.BackgroundColor = Color.CornFlowerBlue;
        }

        public void BeginRun() { }
        public void LoadContent() { }

        public void CreateMenus()
        {
            _mainMenu = Engine.MenuManager.Create("Main Menu");
            var _secondaryMenu = Engine.MenuManager.Create("Menu Number Two");

            _mainMenu.BackgroundColor = Color.White;
            _mainMenu.FrameColor = Color.Silver;
            _mainMenu.MenuItemColor = Color.LightBlue;
            _mainMenu.TitleColor = Color.Red;
            _mainMenu.SelectedItemColor = Color.Navy;
            _mainMenu.SelectedItemBackground = Color.Silver;
            _mainMenu.SelectedItemBorderColor = Color.Grey;

            _mainMenu.AddMenu("Other Menu", _secondaryMenu);
            _mainMenu.AddMenu("Close Menu", MNU_CLOSE);
            _mainMenu.AddMenu("Exit", MNU_EXIT);            
            
            _secondaryMenu.AddMenu("Main Menu", _mainMenu);
            _secondaryMenu.AddMenu("Exit The App", MNU_EXIT);

            _mainMenu.MenuSelected += _mainMenu_MenuSelected;
            _secondaryMenu.MenuSelected += _mainMenu_MenuSelected;

            Engine.RegisterComponent(_mainMenu);
            Engine.RegisterComponent(_secondaryMenu);

            Engine.InputManager.LeftSoftButton = "Main Menu";
            Engine.InputManager.RightSoftButton = "Exit";
        }

        void _mainMenu_MenuSelected(object sender, WiMo.Games.Menus.SelectedMenuEventArgs e)
        {
            switch (e.SelectedMenu.MenuId)
            {
                case MNU_CLOSE:
                    _mainMenu.Hide();
                    Engine.Resume();
                    break;
                case MNU_EXIT:
                    Engine.Exit();
                    break;
            }
            
        }        

        IInputManager _prevState;

        public void Update(GameTime gameTime)
        {
            if (Engine.InputManager.WasPressed(_prevState, Keys.LeftSoftButton))
                _mainMenu.Show();

            if (Engine.InputManager.WasPressed(_prevState, Keys.RightSoftButton))
                Engine.Exit();

            _prevState = Engine.InputManager.GetKeysState();    
        }

        public void Draw(GameTime gameTime)
        {

        }

        public bool CanExit()
        {
            return true;
        }

        public IGameEngine Engine { get; set; }
    }
}
